package org.rsbot.script.methods;

import java.awt.Point;
import org.rsbot.script.internal.InputManager;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;

public class Bank extends MethodProvider
{
  public static final int[] BANKERS = { 44, 45, 494, 495, 499, 958, 1036, 2271, 2354, 2355, 3824, 5488, 5901, 5912, 5913, 6362, 6532, 6533, 6534, 6535, 7605, 8948, 9710, 14367 };

  public static final int[] BANK_BOOTHS = { 2213, 4483, 6084, 11402, 11758, 12759, 14367, 19230, 24914, 25808, 26972, 27663, 29085, 34752, 35647, 36786 };

  public static final int[] BANK_CHESTS = { 4483, 12308, 21301, 27663, 42192 };
  public static final int[] BANK_DEPOSIT_BOX = { 9398, 20228, 26969, 36788 };

  public static final int[] DO_NOT_DEPOSIT = { 1265, 1267, 1269, 1273, 1271, 1275, 1351, 590, 303 };
  public static final int INTERFACE_BANK = 762;
  public static final int INTERFACE_BANK_BUTTON_CLOSE = 43;
  public static final int INTERFACE_BANK_BUTTON_DEPOSIT_BEAST_INVENTORY = 38;
  public static final int INTERFACE_BANK_BUTTON_DEPOSIT_CARRIED_ITEMS = 34;
  public static final int INTERFACE_BANK_BUTTON_DEPOSIT_WORN_ITEMS = 36;
  public static final int INTERFACE_BANK_BUTTON_HELP = 44;
  public static final int INTERFACE_BANK_BUTTON_INSERT = 15;
  public static final int INTERFACE_BANK_BUTTON_ITEM = 19;
  public static final int INTERFACE_BANK_BUTTON_NOTE = 19;
  public static final int INTERFACE_BANK_BUTTON_SEARCH = 17;
  public static final int INTERFACE_BANK_BUTTON_SWAP = 15;
  public static final int INTERFACE_BANK_BUTTON_OPEN_EQUIP = 117;
  public static final int INTERFACE_BANK_INVENTORY = 93;
  public static final int INTERFACE_BANK_ITEM_FREE_COUNT = 29;
  public static final int INTERFACE_BANK_ITEM_FREE_MAX = 30;
  public static final int INTERFACE_BANK_ITEM_MEMBERS_COUNT = 31;
  public static final int INTERFACE_BANK_ITEM_MEMBERS_MAX = 32;
  public static final int INTERFACE_BANK_SCROLLBAR = 114;
  public static final int INTERFACE_BANK_SEARCH = 752;
  public static final int INTERFACE_BANK_SEARCH_INPUT = 5;
  public static final int INTERFACE_EQUIPMENT = 667;
  public static final int INTERFACE_EQUIPMENT_COMPONENT = 7;
  public static final int INTERFACE_COLLECTION_BOX = 105;
  public static final int INTERFACE_COLLECTION_BOX_CLOSE = 13;
  public static final int[] INTERFACE_BANK_TAB = { 63, 61, 59, 57, 55, 53, 51, 49, 47 };

  public static final int[] INTERFACE_BANK_TAB_FIRST_ITEM = { 78, 79, 80, 81, 82, 83, 84, 85, 86 };
  public static final int INTERFACE_DEPOSIT_BOX = 11;
  public static final int INTERFACE_DEPOSIT_BOX_BUTTON_CLOSE = 15;
  public static final int INTERFACE_DEPOSIT_BUTTON_DEPOSIT_BEAST_INVENTORY = 22;
  public static final int INTERFACE_DEPOSIT_BUTTON_DEPOSIT_CARRIED_ITEMS = 18;
  public static final int INTERFACE_DEPOSIT_BUTTON_DEPOSIT_WORN_ITEMS = 20;

  Bank(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public boolean close()
  {
    if (isOpen()) {
      this.methods.interfaces.getComponent(762, 43).doClick();

      sleep(random(500, 600));
      return !isOpen();
    }
    if (isDepositOpen()) {
      this.methods.interfaces.getComponent(11, 15).doClick();

      sleep(random(500, 600));
      return !isDepositOpen();
    }
    return false;
  }

  public boolean deposit(int paramInt1, int paramInt2)
  {
    if ((isOpen()) || (isDepositOpen())) {
      if (paramInt2 < 0) {
        throw new IllegalArgumentException("number < 0 (" + paramInt2 + ")");
      }

      Object localObject = null;
      int i = 0;
      int j = isOpen() ? this.methods.inventory.getCount(true) : getBoxCount();

      if (!isOpen()) {
        k = 0;
        for (int m = 0; m < 28; m++) {
          RSComponent localRSComponent = this.methods.interfaces.get(11).getComponent(17).getComponent(m);

          if (localRSComponent.getComponentID() == paramInt1) {
            i += localRSComponent.getComponentStackSize();
            if (k == 0) {
              localObject = localRSComponent;
              k = 1;
            }
          }
          if (i > 1)
            break;
        }
      }
      else {
        localObject = this.methods.inventory.getItem(new int[] { paramInt1 }).getComponent();
        i = this.methods.inventory.getCount(true, new int[] { paramInt1 });
      }

      if (localObject == null) {
        return true;
      }

      switch (paramInt2) {
      case 0:
        ((RSComponent)localObject).doAction(i > 1 ? "Deposit-All" : "Deposit");
        break;
      case 1:
        ((RSComponent)localObject).doAction("Deposit");
        break;
      case 5:
        ((RSComponent)localObject).doAction("Deposit-" + paramInt2);
        break;
      default:
        if ((((RSComponent)localObject).doAction("Deposit-" + paramInt2)) || 
          (!((RSComponent)localObject).doAction("Deposit-X"))) break;
        sleep(random(1000, 1300));
        this.methods.inputManager.sendKeys(String.valueOf(paramInt2), true);
      }

      sleep(300);
      int k = isOpen() ? this.methods.inventory.getCount(true) : getBoxCount();

      return (k < j) || (k == 0);
    }
    return false;
  }

  public boolean depositAll()
  {
    if (isOpen()) {
      return this.methods.interfaces.getComponent(762, 34).doClick();
    }

    return (isDepositOpen()) && (this.methods.interfaces.getComponent(11, 18).doClick());
  }

  public boolean depositAllExcept(int[] paramArrayOfInt)
  {
    if ((isOpen()) || (isDepositOpen())) {
      int i = 1;
      int j = isOpen() ? this.methods.inventory.getCount(true) : getBoxCount();

      for (int k = 0; k < 28; k++) {
        RSComponent localRSComponent = isOpen() ? this.methods.inventory.getItemAt(k).getComponent() : this.methods.interfaces.get(11).getComponent(17).getComponent(k);

        if ((localRSComponent != null) && (localRSComponent.getComponentID() != -1)) {
          int[] arrayOfInt = paramArrayOfInt; int n = arrayOfInt.length; int i1 = 0;
          while (true) if (i1 < n) { int i2 = arrayOfInt[i1];
              if (localRSComponent.getComponentID() == i2)
                break;
              i1++; continue;
            }
            else
            {
              int m = 0;
              while (true) if (m < 5) {
                  deposit(localRSComponent.getComponentID(), 0);
                  sleep(random(600, 900));
                  n = isOpen() ? this.methods.inventory.getCount(true) : getBoxCount();

                  if (n < j) {
                    j = n;
                  }
                  else
                  {
                    m++; continue;
                  }

                }
                else
                {
                  i = 0;
                } 
            } 
        }
      }
      return i;
    }
    return false;
  }

  public boolean depositAllEquipped()
  {
    if (isOpen()) {
      return this.methods.interfaces.getComponent(762, 36).doClick();
    }

    return (isDepositOpen()) && (this.methods.interfaces.getComponent(11, 20).doClick());
  }

  public boolean depositAllFamiliar()
  {
    if (isOpen()) {
      return this.methods.interfaces.getComponent(762, 38).doClick();
    }
    if (isDepositOpen()) {
      return this.methods.interfaces.getComponent(11, 22).doClick();
    }
    return false;
  }

  public int getCount(int[] paramArrayOfInt)
  {
    int i = 0;
    RSItem[] arrayOfRSItem1 = getItems();
    for (RSItem localRSItem : arrayOfRSItem1) {
      for (int i1 : paramArrayOfInt) {
        if (localRSItem.getID() == i1) {
          i += localRSItem.getStackSize();
        }
      }
    }
    return i;
  }

  public int getCurrentTab()
  {
    for (int i = 0; i < INTERFACE_BANK_TAB.length; i++)
      if (this.methods.interfaces.get(762).getComponent(INTERFACE_BANK_TAB[i] - 1).getBackgroundColor() == 1419)
      {
        return i;
      }
    return -1;
  }

  public RSInterface getInterface()
  {
    return this.methods.interfaces.get(762);
  }

  public RSInterface getBoxInterface()
  {
    return this.methods.interfaces.get(762);
  }

  public RSItem getItemAt(int paramInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getComponent().getComponentIndex() == paramInt) {
          return localRSItem;
        }
      }
    }
    return null;
  }

  public RSItem getItem(int paramInt)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getID() == paramInt)
          return localRSItem;
      }
    }
    return null;
  }

  public Point getItemPoint(int paramInt)
  {
    if (paramInt < 0) {
      throw new IllegalArgumentException("slot < 0 " + paramInt);
    }
    RSItem localRSItem = getItemAt(paramInt);
    if (localRSItem != null) {
      return localRSItem.getComponent().getLocation();
    }
    return new Point(-1, -1);
  }

  public RSItem[] getItems()
  {
    if ((getInterface() == null) || (getInterface().getComponent(93) == null))
    {
      return new RSItem[0];
    }
    RSComponent[] arrayOfRSComponent = getInterface().getComponent(93).getComponents();

    RSItem[] arrayOfRSItem = new RSItem[arrayOfRSComponent.length];
    for (int i = 0; i < arrayOfRSItem.length; i++) {
      arrayOfRSItem[i] = new RSItem(this.methods, arrayOfRSComponent[i]);
    }
    return arrayOfRSItem;
  }

  public boolean isOpen()
  {
    return getInterface().isValid();
  }

  public boolean isDepositOpen()
  {
    return this.methods.interfaces.get(11).isValid();
  }

  public boolean open()
  {
    try
    {
      if (!isOpen()) {
        if (this.methods.menu.isOpen()) {
          this.methods.mouse.moveSlightly();
          sleep(random(20, 30));
        }
        RSObject localRSObject1 = this.methods.objects.getNearest(BANK_BOOTHS);
        RSNPC localRSNPC = this.methods.npcs.getNearest(BANKERS);
        RSObject localRSObject2 = this.methods.objects.getNearest(BANK_CHESTS);

        int i = this.methods.calc.distanceTo(localRSObject1);
        if ((localRSNPC != null) && (this.methods.calc.distanceTo(localRSNPC) < i))
        {
          i = this.methods.calc.distanceTo(localRSNPC);
          localRSObject1 = null;
        }
        if ((localRSObject2 != null) && (this.methods.calc.distanceTo(localRSObject2) < i))
        {
          localRSObject1 = null;
          localRSNPC = null;
        }
        if (((localRSObject1 != null) && (this.methods.calc.distanceTo(localRSObject1) < 5) && (this.methods.calc.tileOnMap(localRSObject1.getLocation())) && (this.methods.calc.canReach(localRSObject1.getLocation(), true))) || ((localRSNPC != null) && (this.methods.calc.distanceTo(localRSNPC) < 8) && (this.methods.calc.tileOnMap(localRSNPC.getLocation())) && (this.methods.calc.canReach(localRSNPC.getLocation(), true))) || ((localRSObject2 != null) && (this.methods.calc.distanceTo(localRSObject2) < 8) && (this.methods.calc.tileOnMap(localRSObject2.getLocation())) && (this.methods.calc.canReach(localRSObject2.getLocation(), true)) && (!isOpen())))
        {
          int j;
          if (localRSObject1 != null) {
            if (localRSObject1.doAction("Use-Quickly")) {
              j = 0;
              while (!isOpen()) { j++; if (j >= 10) break;
                sleep(random(200, 400));
                if (this.methods.players.getMyPlayer().isMoving())
                  j = 0; }
            }
            else
            {
              this.methods.camera.turnToObject(localRSObject1);
            }
          } else if (localRSNPC != null) {
            if (localRSNPC.doAction("Bank ")) {
              j = 0;
              while (!isOpen()) { j++; if (j >= 10) break;
                sleep(random(200, 400));
                if (this.methods.players.getMyPlayer().isMoving())
                  j = 0; }
            }
            else
            {
              this.methods.camera.turnToCharacter(localRSNPC, 20);
            }
          } else if (localRSObject2 != null) {
            if ((localRSObject2.doAction("Bank")) || (this.methods.menu.doAction("Use")))
            {
              j = 0;
              while (!isOpen()) { j++; if (j >= 10) break;
                sleep(random(200, 400));
                if (this.methods.players.getMyPlayer().isMoving())
                  j = 0; }
            }
            else
            {
              this.methods.camera.turnToObject(localRSObject2);
            }
          }
        }
        else if (localRSObject1 != null) {
          this.methods.walking.walkTo(localRSObject1.getLocation());
        } else if (localRSNPC != null) {
          this.methods.walking.walkTo(localRSNPC.getLocation());
        } else if (localRSObject2 != null) {
          this.methods.walking.walkTo(localRSObject2.getLocation());
        }
      }

      return isOpen();
    } catch (Exception localException) {
      localException.printStackTrace();
    }return false;
  }

  public boolean openDepositBox()
  {
    try
    {
      if (!isDepositOpen()) {
        if (this.methods.menu.isOpen()) {
          this.methods.mouse.moveSlightly();
          sleep(random(20, 30));
        }
        RSObject localRSObject = this.methods.objects.getNearest(BANK_DEPOSIT_BOX);

        if ((localRSObject != null) && (this.methods.calc.distanceTo(localRSObject) < 8) && (this.methods.calc.tileOnMap(localRSObject.getLocation())) && (this.methods.calc.canReach(localRSObject.getLocation(), true)))
        {
          if (localRSObject.doAction("Deposit")) {
            int i = 0;
            while (!isDepositOpen()) { i++; if (i >= 10) break;
              sleep(random(200, 400));
              if (this.methods.players.getMyPlayer().isMoving())
                i = 0; }
          }
          else
          {
            this.methods.camera.turnToObject(localRSObject, 20);
          }
        }
        else if (localRSObject != null) {
          this.methods.walking.walkTo(localRSObject.getLocation());
        }
      }

      return isDepositOpen();
    } catch (Exception localException) {
      localException.printStackTrace();
    }return false;
  }

  public boolean openTab(int paramInt)
  {
    return (isOpen()) && (this.methods.interfaces.getComponent(762, INTERFACE_BANK_TAB[(paramInt - 1)]).doClick());
  }

  public boolean isSearchOpen()
  {
    return this.methods.settings.getSetting(1248) == -2147483648;
  }

  public boolean searchItem(String paramString)
  {
    if (!isOpen()) {
      return false;
    }
    this.methods.interfaces.getComponent(762, 17).doAction("Search");

    sleep(random(1000, 1500));
    if (!isSearchOpen()) {
      sleep(500);
    }
    if ((isOpen()) && (isSearchOpen())) {
      this.methods.inputManager.sendKeys(paramString, false);
      sleep(random(300, 700));
      return true;
    }
    return false;
  }

  public boolean setRearrangeModeToInsert()
  {
    if (!isOpen())
      return false;
    if (this.methods.settings.getSetting(304) != 1)
    {
      this.methods.interfaces.getComponent(762, 15).doClick();

      sleep(random(500, 700));
    }
    return this.methods.settings.getSetting(304) == 1;
  }

  public boolean setRearrangeModeToSwap()
  {
    if (!isOpen())
      return false;
    if (this.methods.settings.getSetting(304) != 0)
    {
      this.methods.interfaces.getComponent(762, 15).doClick();

      sleep(random(500, 700));
    }
    return this.methods.settings.getSetting(304) == 0;
  }

  public boolean setWithdrawModeToItem()
  {
    if (!isOpen())
      return false;
    if (this.methods.settings.getSetting(115) != 0)
    {
      this.methods.interfaces.getComponent(762, 19).doClick();

      sleep(random(500, 700));
    }
    return this.methods.settings.getSetting(115) == 0;
  }

  public boolean setWithdrawModeToNote()
  {
    if (!isOpen())
      return false;
    if (this.methods.settings.getSetting(115) != 1)
    {
      this.methods.interfaces.getComponent(762, 19).doClick();

      sleep(random(500, 700));
    }
    return this.methods.settings.getSetting(115) == 1;
  }

  public boolean withdraw(int paramInt1, int paramInt2)
  {
    if (isOpen()) {
      if (paramInt2 < 0)
        throw new IllegalArgumentException("count (" + paramInt2 + ") < 0");
      RSItem localRSItem = getItem(paramInt1);
      if (localRSItem == null)
        return false;
      RSComponent localRSComponent = localRSItem.getComponent();
      if (localRSComponent == null) {
        return false;
      }

      while ((localRSComponent.getRelativeX() == 0) && (getCurrentTab() != 0)) {
        this.methods.interfaces.getComponent(762, INTERFACE_BANK_TAB[0]).doClick();

        sleep(random(800, 1300));
      }

      if (!this.methods.interfaces.scrollTo(localRSComponent, 49938546))
      {
        return false;
      }
      int i = this.methods.inventory.getCount(true);
      switch (paramInt2) {
      case 0:
        localRSComponent.doAction("Withdraw-All");
        break;
      case 1:
        localRSComponent.doClick(true);
        break;
      case 5:
      case 10:
        localRSComponent.doAction("Withdraw-" + paramInt2);
        break;
      default:
        if ((localRSComponent.doAction("Withdraw-" + paramInt2)) || 
          (!localRSComponent.doAction("Withdraw-X"))) break;
        sleep(random(1000, 1300));
        this.methods.keyboard.sendText(String.valueOf(paramInt2), true);
      }

      sleep(random(1000, 1300));
      int j = this.methods.inventory.getCount(true);
      return (j > i) || (j == 28);
    }
    return false;
  }

  public int getBoxCount(int[] paramArrayOfInt)
  {
    if (!isDepositOpen()) {
      return -1;
    }
    int i = 0;
    for (int j = 0; j < 28; j++) {
      for (int n : paramArrayOfInt) {
        if ((!this.methods.interfaces.get(11).getComponent(17).isValid()) || (this.methods.interfaces.get(11).getComponent(17).getComponent(j).getComponentID() != n)) {
          continue;
        }
        i++;
      }
    }

    return i;
  }

  public int getBoxCount()
  {
    if (!isDepositOpen()) {
      return -1;
    }
    int i = 0;
    for (int j = 0; j < 28; j++) {
      if ((!this.methods.interfaces.get(11).getComponent(17).isValid()) || (this.methods.interfaces.get(11).getComponent(17).getComponent(j).getComponentID() == -1)) {
        continue;
      }
      i++;
    }

    return i;
  }

  public RSItem[] getEquipmentItems()
  {
    if (this.methods.interfaces.get(667).getComponent(7).isValid())
    {
      return new RSItem[0];
    }
    RSComponent[] arrayOfRSComponent = this.methods.interfaces.get(667).getComponent(7).getComponents();

    RSItem[] arrayOfRSItem = new RSItem[arrayOfRSComponent.length];
    for (int i = 0; i < arrayOfRSItem.length; i++) {
      arrayOfRSItem[i] = new RSItem(this.methods, arrayOfRSComponent[i]);
    }
    return arrayOfRSItem;
  }

  public RSItem getEquipmentItem(int paramInt)
  {
    RSItem[] arrayOfRSItem1 = getEquipmentItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getID() == paramInt)
          return localRSItem;
      }
    }
    return null;
  }

  public int getEquipmentItemID(String paramString)
  {
    RSItem[] arrayOfRSItem1 = getEquipmentItems();
    if (arrayOfRSItem1 != null) {
      for (RSItem localRSItem : arrayOfRSItem1) {
        if (localRSItem.getName().contains(paramString))
          return localRSItem.getID();
      }
    }
    return -1;
  }

  public boolean openEquipment()
  {
    if (!getInterface().getComponent(117).isValid())
    {
      return false;
    }return getInterface().getComponent(117).doClick();
  }

  public int getItemID(String paramString)
  {
    RSItem[] arrayOfRSItem1 = getItems();
    if (arrayOfRSItem1 != null)
    {
      RSItem localRSItem;
      for (localRSItem : arrayOfRSItem1) {
        if (localRSItem.getName().toLowerCase().equals(paramString.toLowerCase())) {
          return localRSItem.getID();
        }
      }
      for (localRSItem : arrayOfRSItem1) {
        if (localRSItem.getName().toLowerCase().contains(paramString.toLowerCase())) {
          return localRSItem.getID();
        }
      }
    }
    return -1;
  }
}